package com.example.message.mvvm.fragment

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.message.adapter.MessageAdapter
import com.example.message.bean.MessItemBean
import com.example.module.R
import com.example.module.message.mvvm.viewmodel.MessageViewModel
import com.gsafety.libraryktx.base.BaseActivity
import com.gsafety.libraryktx.base.BaseFragment
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.bean.WarnData
import com.gsafety.libraryktx.core.RouterConstants
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.expand.toast
import com.gsafety.libraryktx.util.RecyclerViewHelper
import com.gsafety.libraryktx.util.StatusUtils
import com.gsafety.libraryktx.widget.CommonRefreshView
import kotlinx.android.synthetic.main.message_fragment_layout.*

/**消息
 * @author : lj
 * @date : 2020/12/29 16:22
 */
@Route(path = RouterConstants.MESSAGE_FRAGMENT)
class MessageFragment: BaseFragment(), CommonRefreshView.RefreshListener {
    private var mAdapter: MessageAdapter? = null
    private var datas: MutableList<WarnData.RecordsBean> = ArrayList()
    private lateinit var mv: MessageViewModel
    var pageSize = 10
    var current = 1
    var currentAttendPos = 0
    var mEtSearch: EditText? = null
    override fun initView(view: View) {
        var title=view.findViewById<TextView>(R.id.title_name)
        var toolbar=view.findViewById<Toolbar>(R.id.toolbar)
        toolbar.navigationIcon=null
        title.text=resources.getText(R.string.message_title)
        StatusUtils.titleBar(this,toolbar)
        crv_common_refresh.mRefreshListener = this
        mEtSearch = view?.findViewById<EditText>(R.id.et_search)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initmodel()
    }

    override fun initData() {
        var rv_msg=view?.findViewById<RecyclerView>(R.id.rv_msg)
        mAdapter = MessageAdapter(datas)
        RecyclerViewHelper.initRecyclerViewV(mContext, rv_msg!!, false, mAdapter)
        mEtSearch?.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                onRefresh()
            }
            false
        }

        mAdapter?.onItemAttentionClickListener = object:MessageAdapter.OnItemAttentionClickListener{
            override fun onItemAttendClick(pos: Int) {
                var item = datas.get(pos)
                currentAttendPos = pos
                if(item.attended){
                    mv.cancelAttention(item.pid!!)
                }else{
                    mv.setAttention(item.pid!!)
                }
            }
        }

    }

    override fun onResume() {
        super.onResume()
        crv_common_refresh.autoRefresh()
    }

    override fun onRefresh() {
        current = 1
        datas.clear()
        mv.requestWarnList(pageSize,current,"",mEtSearch?.text.toString())
    }

    override fun onLoadMore() {
        current++
        mv.requestWarnList(pageSize,current,"",mEtSearch?.text.toString())
    }

    override fun getLayoutId(): Int {
        return R.layout.message_fragment_layout
    }

    private fun initmodel(){
        mv = MessageViewModel()
        mv = create(MessageViewModel::class)
        mv.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Success -> {
                    crv_common_refresh.finishRefresh()
                    mv.loadValue.value.let {it->
                        datas.addAll(it?.records!!)
                        mAdapter?.setNewData(datas)
                    }
                }
                is LoadState.Fail -> {
                    (activity as BaseActivity).toast(it.msg)
                    crv_common_refresh.finishRefresh()
                }
            }
        })
        mv.setAttendValue.observe(this, Observer {
            if (mv.setAttendValue.value == true) {
                datas.get(currentAttendPos).attended = true
                mAdapter?.notifyDataSetChanged()
                Toast.makeText(activity, "关注成功！", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(activity, "关注失败！", Toast.LENGTH_SHORT).show()
            }
        })

        mv.cancelAttendValue.observe(this, Observer {
            if (mv.cancelAttendValue.value == true) {
                datas.get(currentAttendPos).attended = false
                mAdapter?.notifyDataSetChanged()
                Toast.makeText(activity, "取消关注成功！", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(activity, "取消关注失败！", Toast.LENGTH_SHORT).show()
            }
        })
    }
}